package com.ruoyi.base.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.base.domain.Subject;
import com.ruoyi.base.domain.dto.SubjectReqDTO;
import com.ruoyi.base.mapper.SubjectMapper;
import com.ruoyi.base.service.ISubjectService;
import com.ruoyi.common.core.utils.number.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 科目管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-21
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper,Subject> implements ISubjectService
{
    @Autowired
    private SubjectMapper financialSubjectMapper;

    /**
     * 查询科目管理
     * 
     * @param id 科目管理主键
     * @return 科目管理
     */
    @Override
    public Subject selectFinancialSubjectById(Long id)
    {
        return financialSubjectMapper.selectSubjectById(id);
    }

    /**
     * 查询科目管理列表
     * 
     * @param financialSubject 科目管理
     * @return 科目管理
     */
    @Override
    public List<Subject> selectFinancialSubjectList(Subject financialSubject)
    {
        return financialSubjectMapper.selectSubjectList(financialSubject);
    }

    /**
     * 新增科目管理
     * 
     * @param financialSubject 科目管理
     * @return 结果
     */
    @Override
    public int insertFinancialSubject(Subject financialSubject)
    {
        return financialSubjectMapper.insert(financialSubject);
    }

    /**
     * 修改科目管理
     * 
     * @param financialSubject 科目管理
     * @return 结果
     */
    @Override
    public int updateFinancialSubject(Subject financialSubject)
    {
        return financialSubjectMapper.updateById(financialSubject);
    }

    /**
     * 批量删除科目管理
     * 
     * @param ids 需要删除的科目管理主键
     * @return 结果
     */
    @Override
    public int deleteFinancialSubjectByIds(Long[] ids)
    {
        return financialSubjectMapper.deleteSubjectByIds(ids);
    }

    /**
     * 删除科目管理信息
     * 
     * @param id 科目管理主键
     * @return 结果
     */
    @Override
    public int deleteFinancialSubjectById(Long id)
    {
        return financialSubjectMapper.deleteSubjectById(id);
    }

    /**
    * @Description: 查询科目列表
    * @Param: [subjectReqDto]
    * @return: java.util.List<com.ruoyi.base.domain.Subject>
    * @Author: Yuan
    */
    @Override
    public List<Subject> selectSubjectList(SubjectReqDTO subjectReqDto) {
        Subject subject = new Subject();
        if (subjectReqDto.getSubjectType() != null) {
            subject.setSubjectType(subjectReqDto.getSubjectType());
        }
        if (subjectReqDto.getValues() != null) {
            if (NumberUtils.isNumberString(subjectReqDto.getValues())) {
                subject.setCode(subjectReqDto.getValues());
            } else {
                subject.setName(subjectReqDto.getValues());
            }
        }
        return financialSubjectMapper.selectSubjectList(subject);
    }

    /**
    * @Description: 查询科目树形结构
    * @Param: []
    * @return: java.util.List<com.ruoyi.base.domain.Subject>
    * @Author: Yuan
    */
    public List<Subject> selectSubjectTree(){
        List<Subject> subjectList = financialSubjectMapper.selectList(new LambdaQueryWrapper<Subject>()
                .eq(Subject::getIsDel, true).orderByAsc(Subject::getCode));
        return buildSubjectTree(subjectList,0L);

    }

    /**
    * @Description: 构建科目树
    * @Param: [subjectList, pid]
    * @return: java.util.List<com.ruoyi.base.domain.Subject>
    * @Author: Yuan
    */
    private List<Subject> buildSubjectTree(List<Subject> subjectList,Long pid){
        List<Subject> treeList = new ArrayList<>();
        for (Subject subject : subjectList) {
            if (subject.getPid().equals(pid)){
                subject.setChildren(buildSubjectTree(subjectList,subject.getId()));
                treeList.add(subject);
            }
        }
        return treeList;
    }

    /**
    * @Description: 查询上级科目选择列表
    * @Param: []
    * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
    * @Author: Yuan
    */
    public List<Map<String, Object>> selectParentSubjectOptions() {
        List<Subject> subjects = financialSubjectMapper.selectList(
                new LambdaQueryWrapper<Subject>()
                        .eq(Subject::getIsDel, true)
                        .orderByAsc(Subject::getCode)
        );

        return subjects.stream().map(subject -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", subject.getId());
            map.put("label", subject.getName());
            map.put("code", subject.getCode());
            return map;
        }).collect(Collectors.toList());
    }
}
